package com.example.androidlearning.sy5;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.androidlearning.R;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class sy5Activity03 extends Activity {

    private TextView show;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sy0503_async);
        show = findViewById(R.id.async_text);
    }

    public void downLoad(View source) throws MalformedURLException{
        DownTask task = new DownTask(this);
        task.execute(
                new URL("https://www.baidu.com/img/bd_logo1.png?where=super,https://www.baidu.com/img/bd_logo1.png?where=super"),
                new URL("https://gss0.bdstatic.com/5bVWsj_p_tVS5dKfpU_Y_D3/res/r/image/2017-09-27/297f5edb1e984613083a2d3cc0c5bb36.png")
        );
    }

    class DownTask extends AsyncTask<URL, Integer, Bitmap[]>{

        ProgressDialog pdialog;
        Context context;
        DownTask(Context context){
            this.context = context;
        }

        @Override
        protected Bitmap[] doInBackground(URL... urls) {
            Bitmap[] bitmap = new Bitmap[urls.length];
            try {
                for(int i = 0; i < urls.length; i++){
                    URL imgUrl = urls[i];
                    HttpURLConnection conn = (HttpURLConnection) imgUrl
                            .openConnection();
                    conn.setDoInput(true);
                    conn.connect();
                    InputStream is = conn.getInputStream();
                    bitmap[i] = BitmapFactory.decodeStream(is);
                    publishProgress((int) (100 * (i + 1 / (float)urls.length)));
                    is.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap[] bitmap) {
            ImageView imageView;
            LinearLayout layout = findViewById(R.id.sy0503_ll);
            for(Bitmap b : bitmap){
                imageView = new ImageView(context);
                imageView.setImageBitmap(b);
                layout.addView(imageView);
            }
            pdialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            pdialog = new ProgressDialog(context);
            pdialog.setTitle("任务正在执行中");
            pdialog.setMessage("任务正在执行中，敬请等待...");
            pdialog.setCancelable(false);
            pdialog.setMax(100);
            pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pdialog.setIndeterminate(false);
            pdialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            pdialog.setProgress(values[0]);
        }
    }

}
